
<div class="easyui-layout" data-options="fit:true"  style="margin:5px">
    <!-- Begin of toolbar -->
    <div data-options="region:'center',border:false">
    <!-- Begin of 工具条 -->
    <div id="menu-toolbar" style="margin:5px">
        <div>
         <@shiro.hasPermission name="Menu:create"> 
        	<a href="javascript:;" class="easyui-linkbutton" iconCls="icon-add" onclick="openAddMenu()" plain="false">添加</a>
         </@shiro.hasPermission>
         <@shiro.hasPermission name="Menu:update"> 
            <a href="javascript:;" class="easyui-linkbutton" iconCls="icon-edit" onclick="openEditMenu()" plain="false">修改</a>
          </@shiro.hasPermission>   
         <@shiro.hasPermission name="Menu:delete"> 
            <a href="javascript:;" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeMenu()" plain="false">删除</a>
          </@shiro.hasPermission>  
            &nbsp;<label>菜单名称：</label><input  class="easyui-textbox" id="uname"  style="width:100px">
            
            <a href="javascript:;" id ="menu-search" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
        </div>
    </div>
    <!-- end of 工具条-->
	<table id="menu-datagrid"  ></table>
    </div>
</div>
<!-- Begin of 新增、编辑 -->
<div id="menu-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
	<form id="menu-form" method="post">
        <table>
            <tr>
                <td width="100" align="right">菜单名称:</td>
                <td><input data-options="required:true" type="text" name="menuName" class="easyui-textbox" /></td>
            </tr>
            <tr>
                <td align="right">菜单链接:</td>
                <td><input data-options="required:true" type="text" name="menuUrl" class="easyui-textbox" /></td>
            </tr>
             <tr>
                <td align="right">权限名称:</td>
                <td><input data-options="required:true" type="text" name="permission" class="easyui-textbox" /></td>
            </tr>
            <tr>
                <td align="right">图标样式:</td>
                <td>
                	<input data-options="required:true" type="text" name="iconUrl" class="easyui-textbox" />
                	<!-- 菜单-类型 -->
                </td>
            </tr>
            <tr>
                <td align="right">单独iframe:</td>
                <td>
                	<select data-options="required:true,editable:false"  name="isiframe" class="easyui-combobox" style="width:150px">
                		<option value="1">是</option>
                		<option value="0">否</option>
                	</select>
                	<!-- 菜单-类型 -->
                </td>
            </tr>
             <tr>
                <td align="right">按钮权限:</td>
                <td>
                	<input  style="width:200px" type="text" id="btnlist" name="buttonsIds" class="easyui-textbox" />
                	<!-- 菜单-类型 -->
                </td>
            </tr>
        </table>
        <input type="hidden" name="menuId"/>
         <input type="hidden" class="form-control" id="pid" name="parentMenuId" > 
         <input type="hidden" class="form-control" id="pids" name="parentMenuIds" > 
    </form>
</div>
<!-- End of 新增、编辑 -->
<script type="text/javascript">

	$(function(){
		//搜索
		$("#menu-search").click(function(){
			$("#menu-datagrid").treegrid('load', {
				menuName: $('#uname').val()
			});
		});
		//按钮
		 $('#btnlist').combobox({
	       url:"buttons/getButtons.html",
	       valueField:'id',
	       textField:'text',
	       multiple:true,
	       prompt:"请选择按钮权限"
	  	 });
	})
	/**
	*  删除记录
	*/
		function removeMenu(){
		var items = $('#menu-datagrid').datagrid('getSelections');
		if(items.length >0){
			$.messager.confirm('信息提示','确定要删除该菜单？', function(result){
				if(result){
					var ids = "";
					$(items).each(function(){
						ids += this.id;
					});
					$.ajax({
						url:'menu/delMenu',
						data:{"menu_id":ids,"available":"1"},
						success:function(data){
							if(data){
								$('#menu-datagrid').treegrid('reload'); 	
							}
							else
							{
								$.messager.alert('信息提示','删除失败！','info');		
							}
						}	
					});
				}	
			});
		}else{
			$.messager.alert('信息提示','请至少选择一条记录！','info');		
		}
	}
	
	/**
	*  打开添加窗口
	*/
	function openAddMenu(){
		$('#btnlist').combobox('reload');
		$('#menu-form').form('clear');
		var node = $('#menu-datagrid').treegrid('getSelected');
		if(node){
			$("#pid").val(node.id);
			$("#pids").val(node.pids);
		}else{
			//不选中默认添加根节点
			$("#pid").val(0);
			$("#pids").val("");
		}
			
			$('#menu-dialog').dialog({
				closed: false,
				modal:true,
	            title: "菜单新增",
	            buttons: [{
	                text: '确定',
	                iconCls: 'icon-ok',
	                handler:addMenu
	            }, {
	                text: '取消',
	                iconCls: 'icon-cancel',
	                handler: function () {
	                    $('#menu-dialog').dialog('close');                    
	                }
	            }]
	        });
// 		}else{
// 			$.messager.alert('信息提示','请选择一个父节点！','info');
// 		}
	}
	
	/**
	* 用户 打开修改窗口
	*/
	function openEditMenu(){
		$('#btnlist').combobox('reload');
		$('#menu-form').form('clear');
		var item = $('#menu-datagrid').treegrid('getSelected');
		if(item){
			//加载表单数据
			$('#menu-form').form('load', "menu/getMenuById?uid="+item.id);
			$('#menu-dialog').dialog({
				closed: false,
				modal:true,
	            title: "菜单修改",
	            buttons: [{
	                text: '确定',
	                iconCls: 'icon-ok',
	                handler: editMenu
	            }, {
	                text: '取消',
	                iconCls: 'icon-cancel',
	                handler: function () {
	                    $('#menu-dialog').dialog('close');                    
	                }
	            }]
	        });
		}else{
			$.messager.alert('信息提示','请选中一条记录进行修改！','info');	
		}
		
	}	
	
	
	/**
	*  添加记录
	*curId 当前选中节点id
	*/
	function addMenu(){
		$('#menu-form').form('submit', {
			url:'menu/addMenu',
			success:function(data){
				if(data){
					$.messager.alert('信息提示','提交成功！','info');
					$('#menu-dialog').dialog('close');
					$('#menu-datagrid').treegrid('reload');  
				}
				else
				{
					$.messager.alert('信息提示','提交失败！','info');
				}
			}
		});
	}
	
	/**
	*  修改记录
	*/
	function editMenu(){
		$('#menu-form').form('submit', {
			url:'menu/editMenu',
			success:function(data){
				if(data){
					$.messager.alert('信息提示','提交成功！','info');
					$('#menu-dialog').dialog('close');
					$('#menu-datagrid').treegrid('reload');  
				}
				else
				{
					$.messager.alert('信息提示','提交失败！','info');
				}
			}
		});
	}
	
	
	/**
	* 载入数据
	*/
	$("#menu-datagrid").treegrid({
		url:'menu/menuGrid',
	    idField:'id',
	    treeField:'title',
		toolbar: "#menu-toolbar",
		rownumbers: true,
		singleSelect:true,
		fit:true,
		columns:[[
			{ field:'title',title:'名称',width:300},
			{ field:'href',title:'链接',width:300},
			{ field:'icon',title:'图标',width:200},
			{ field:'permission',title:'权限名称',width:200},
			{ field:'isiframe',title:'单独iframe',width:200,
				formatter: function(value,row,index){
					if(value == "0"){
						return "否";
					}else{
						return "<font color='#d15b47'>是</font>";
					}
				}	
			}
		]]
	});
	
</script>
